﻿using System;
using System.Collections.Generic;
using SqlParameter = System.Data.SqlClient.SqlParameter;
using SqlDbType = System.Data.SqlDbType;
using IDataReader = System.Data.IDataReader;

namespace GamePlusClassLibrary
{
    public class JogoTags
    {
        public int? Idf_Jogo_Tag { get; set; }
        public int Idf_Jogo { get; set; }
        public int Idf_Tag { get; set; }
        public bool Flg_Principal { get; set; }

        public void Salvar()
        {
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Idf_Jogo", SqlDbType.Int));
            param.Add(new SqlParameter("@Idf_Tag", SqlDbType.Int));
            param.Add(new SqlParameter("@Flg_Principal", SqlDbType.Bit));
            param[0].Value = this.Idf_Jogo;
            param[1].Value = this.Idf_Tag;
            param[2].Value = this.Flg_Principal;

            string sQuery = @"INSERT INTO JogoTags (Idf_Jogo, Idf_Tag, Flg_Principal)
            VALUES (@Idf_Jogo, @Idf_Tag, @Flg_Principal)
            SELECT SCOPE_IDENTITY()";

            object id = DataAccessLayer.ExecuteScalar(System.Data.CommandType.Text, sQuery, param);
            this.Idf_Jogo_Tag = int.Parse(id.ToString());
        }

        public static List<JogoTags> Listar(int pIdJogo)
        { 
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Idf_Jogo", SqlDbType.Int));
            param[0].Value = pIdJogo;

            string sQuery = @"SELECT * FROM JogoTags WHERE Idf_Jogo = @Idf_Jogo";

            List<JogoTags> ls = new List<JogoTags>();

            using (IDataReader iDr = DataAccessLayer.ExecuteReader(System.Data.CommandType.Text, sQuery, param))
            {
                while (iDr.Read())
                {
                    JogoTags obj = new JogoTags();
                    obj.Idf_Jogo_Tag = int.Parse(iDr["Idf_Jogo_Tag"].ToString());
                    obj.Idf_Jogo = int.Parse(iDr["Idf_Jogo"].ToString());
                    obj.Idf_Tag = int.Parse(iDr["Idf_Tag"].ToString());
                    obj.Flg_Principal = bool.Parse(iDr["Flg_Principal"].ToString());
                    ls.Add(obj);
                }
            }

            return ls;
        }

        public static JogoTags CarregarPrincipal(int pIdJogo)
        {
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Idf_Jogo", SqlDbType.Int));
            param[0].Value = pIdJogo;

            string sQuery = @"SELECT * FROM JogoTags WHERE Flg_Principal = 'True' AND Idf_Jogo = @Idf_Jogo";

            JogoTags obj = null;

            using (IDataReader iDr = DataAccessLayer.ExecuteReader(System.Data.CommandType.Text, sQuery, param))
            {
                while (iDr.Read())
                {
                    obj = new JogoTags();
                    obj.Idf_Jogo_Tag = int.Parse(iDr["Idf_Jogo_Tag"].ToString());
                    obj.Idf_Jogo = int.Parse(iDr["Idf_Jogo"].ToString());
                    obj.Idf_Tag = int.Parse(iDr["Idf_Tag"].ToString());
                    obj.Flg_Principal = bool.Parse(iDr["Flg_Principal"].ToString());
                }
            }

            return obj;
        }

        public static JogoTags Carregar(int pIdJogoTag)
        {
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Idf_Jogo_Tag", SqlDbType.Int));
            param[0].Value = pIdJogoTag;

            string sQuery = @"SELECT * FROM JogoTags WHERE Idf_Jogo_Tag = @Idf_Jogo_Tag";

            JogoTags obj = null;

            using (IDataReader iDr = DataAccessLayer.ExecuteReader(System.Data.CommandType.Text, sQuery, param))
            {
                while (iDr.Read())
                {
                    obj = new JogoTags();
                    obj.Idf_Jogo_Tag = int.Parse(iDr["Idf_Jogo_Tag"].ToString());
                    obj.Idf_Jogo = int.Parse(iDr["Idf_Jogo"].ToString());
                    obj.Idf_Tag = int.Parse(iDr["Idf_Tag"].ToString());
                    obj.Flg_Principal = bool.Parse(iDr["Flg_Principal"].ToString());
                }
            }

            return obj;
        }
    }
}
